###################################################################################################
# Term Limits and Legislative Polarization ########################################################
# Michael Olson and Jon Rogowski ##################################################################
###################################################################################################

# This script generates results presented in Table B.9 in the supplementary materials.

###################################################################################################
# working directory and packages ##################################################################
###################################################################################################

  #setwd("./termlimits_polarization_analysisdata")

###################################################################################################
# LEADERSHIP Data #################################################################################
###################################################################################################

  leadership <- read.csv("leadership_only.csv")

# make party variable

  leadership$party <- str_sub(leadership$caucus_year,-1)

# make chamber variable

  leadership$chamber <- str_sub(leadership$caucus_year,9,-3)
  
# make an alternative-named outcome
  
  leadership$np_score_dem <- leadership$np_score

# make professionalims logged

  leadership$legprofscore <- log(leadership$legprofscore)

# make chamber-specific abs_diff

  leadership$abs_diff <- leadership$hs_abs_diff
  leadership$abs_diff[leadership$chamber=="senate"] <- leadership$sen_abs_diff[leadership$chamber=="senate"]

  fe_leader_allcov_rep <- felm(np_score~term_limit*Leader
                               +divided_gov
                               +legprofscore+abs_diff
                               |state+year|0|state
                               ,data=leadership[leadership$party=="R",])
  
  fe_leader_allcov_dem <- felm(np_score_dem~term_limit*Leader
                               +divided_gov
                               +legprofscore+abs_diff
                               |state+year|0|state
                               ,data=leadership[leadership$party=="D",])
  
  
  fe_leader_allcov_house_rep <- felm(np_score~term_limit*Leader
                               +divided_gov
                               +legprofscore+abs_diff
                               |state+year|0|state
                               ,data=leadership[leadership$party=="R" & leadership$chamber=="house",])
  
  fe_leader_allcov_house_dem <- felm(np_score_dem~term_limit*Leader
                               +divided_gov
                               +legprofscore+abs_diff
                               |state+year|0|state
                               ,data=leadership[leadership$party=="D"  & leadership$chamber=="house",])
  
  fe_leader_allcov_senate_rep <- felm(np_score~term_limit*Leader
                                     +divided_gov
                                     +legprofscore+abs_diff
                                     |state+year|0|state
                                     ,data=leadership[leadership$party=="R" & leadership$chamber=="senate",])
  
  fe_leader_allcov_senate_dem <- felm(np_score_dem~term_limit*Leader
                                     +divided_gov
                                     +legprofscore
                                     +abs_diff
                                     |state+year|0|state
                                     ,data=leadership[leadership$party=="D"  & leadership$chamber=="senate",])
  
  leader_sg <- stargazer(fe_leader_allcov_dem,fe_leader_allcov_house_dem,fe_leader_allcov_senate_dem,
                         fe_leader_allcov_rep,fe_leader_allcov_house_rep,fe_leader_allcov_senate_rep,
                         keep=c("term_limit","Leader",
                                "divided_gov","gov_party","legprofscore","abs_diff"),
                         add.lines = list(c("Chamber","Pooled","House","Senate","Pooled","House","Senate"),
                                            c("State Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                          c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                          c("Projected $R^2$",
                                            round(summary(fe_leader_allcov_dem)$P.r.squared,3),round(summary(fe_leader_allcov_house_dem)$P.r.squared,3),
                                            round(summary(fe_leader_allcov_senate_dem)$P.r.squared,3),round(summary(fe_leader_allcov_rep)$P.r.squared,3),
                                            round(summary(fe_leader_allcov_house_rep)$P.r.squared,3),round(summary(fe_leader_allcov_senate_rep)$P.r.squared,3))),
                         notes.append = FALSE,notes.label = "",
                         notes="\\parbox[t]{0.95\\textwidth}{\\footnotesize\\textit{Note}: Entries are linear regression coefficients with 
                         standard errors clustered on states in parentheses. $^{**}$p$<$0.05, $^*$p$<$0.10 (two-tailed test).}",  
                         omit.stat = c("rsq", "f", "ser","adj.rsq"),
                         dep.var.labels = c("Democrats","Republicans"),dep.var.caption = "Shor-McCarty Ideal Point",
                         star.char=c("*","**"),star.cutoffs = c(0.10,0.05),digits=3,digits.extra=0,
                         covariate.labels=c("Term Limits","Leader","Divided Gov.","ln(Leg. Professionalism)","Party Competitiveness",
                                            "Term Limit $\\times$ Leader"),table.placement = "!ht",
                         label="individual_leaders_results",
                         table.layout ="-ld-#-t-as-n",
                         title="Term Limits and Polarization: Heterogeneity by Leadership Status")
  
  cat(leader_sg, sep = '\n', file = paste(output_path,"leader.tex",sep=""))

# reset working directory
  
  setwd(storewd)
  